package com.example.lab3.api.controller.v1;

import com.example.lab3.api.dto.EmployeeDTO;
import com.example.lab3.domain.entity.Employee;
import com.example.lab3.infra.constant.Constants;
import com.example.lab3.infra.utils.LocaleUtil;
import com.example.lab3.service.EmployeeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/v1/employee")
public class EmployeeController {

    // setter注入
    // 构造器注入
    // @Autowired
    // @Resource

    // alt+enter
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @GetMapping("/list")
    public ResponseEntity<?> list(EmployeeDTO employeeDTO) {
        List<Employee> employeeList = employeeService.listEmployees(employeeDTO);
        return ResponseEntity.ok(employeeList);
    }

    @GetMapping("/{employeeId}")
    public ResponseEntity<?> list(@PathVariable("employeeId") Integer employeeId) {
        Employee employee = employeeService.findEmployeeById(employeeId);
        return ResponseEntity.ok(employee);
    }

    @GetMapping("/get-employee-with-leader")
    public ResponseEntity<?> getEmployeeWithLeader(EmployeeDTO employeeDTO) {
        Employee employee = employeeService.getEmployeeWithLeader(employeeDTO);
        return ResponseEntity.ok(employee);
    }

    @GetMapping("/get-employee-list")
    public ResponseEntity<?> getEmployeeList(EmployeeDTO employeeDTO) {
        List<Employee> employee = employeeService.getEmployeeList(employeeDTO);
        return ResponseEntity.ok(employee);
    }

    @PutMapping("/update-employee")
    public ResponseEntity<?> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        if (Objects.isNull(employeeDTO.getEmployeeId())) {
            throw new IllegalArgumentException(LocaleUtil.getLocaleMessage(Constants.ERROR_CODE.Illegal_Argument));
        }
        employeeService.updateEmployee(employeeDTO);
        return ResponseEntity.ok(null);
    }


    @PutMapping("/insert-employee")
    public ResponseEntity<?> insertEmployee(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.insertEmployee(employeeDTO);
        return ResponseEntity.ok(null);
    }

//    {
//        "firstName": "Tom",
//            "lastName": "Jerry",
//            "email": "Tom&Jerry@gmail.com",
//            "phoneNumber": "119",
//            "hireDate": "2023-03-24",
//            "jobId": "manager",
//            "salary": 10000
//    }

}
